<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/27
 * Time: 11:07
 */
require('../extend/phpMQTT/phpMQTT.php');


class Pub{
    protected $db = '';
    protected $car = [];
    protected $pinyin = '';
    public function __construct()
    {
        //$this->pinyin = new \Pin();

        $dbc = array(
            'hostname' => '180.97.75.136',
            'username' => 's',
            'password' => 'wTyw3pwLDtsTGEfC',
            'database' => 's'
        );

        $mysqlnd = function_exists('mysqli_fetch_all');

        if ($mysqlnd) {
            $database = $dbc['database'];
            $db_hostname = $dbc['hostname'];
            $db_username = $dbc['username'];
            $db_password = $dbc['password'];
            $dsn = "mysql:dbname=$database;host=$db_hostname";
            $db = new PDO($dsn, $db_username, $db_password);
            $db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, 0);
            $this->db = $db;
            $res = $db->query("SELECT `IMEI`,`pinyin` FROM `fa_car` WHERE ( IMEI!='' )");

            while ($row = $res -> fetch(PDO::FETCH_ASSOC)){
                $this->car[$row['IMEI']] = $row['pinyin'];
            }

        }
        else
        {
            echo '连接失败';exit;
        }
    }

    public function connect($client_id)
    {
        $server = 'develop.rltest.cn';     // change if necessary
        $port = 1883;                     // change if necessary

        //$client_id = $client_id; // make sure this is unique for connecting to sever - you could use uniqid()
        //Loader::import('phpmqtt/phpMQTT', EXTEND_PATH);

        $mqtt = new \phpmqtt\phpMQTT($server, $port, $client_id);

        $mqtt->debug = true;
        return $mqtt;
    }

    //获取学生上下车打开位置时间
    public function info()
    {
        $client_id = time().rand();

        $mqtt = $this->connect($client_id);
        $username = 'rl517';                   // set your username
        $password = 'rlian2022';                   // set your password
        if(!$mqtt->connect(true, NULL, $username, $password)) {
            echo '链接失败';exit;
        }
        $topics_name = "ScBusTem/RCInfoMsg";
        //$topics['ScBusTem/DevRegularInfo'] = array('qos' => 0, 'function' => 'procMsg');
        $topics[$topics_name] = array("qos" => 0, "function" =>array($this,"onMessageInfo"));
        $mqtt->subscribe($topics, 0);


        while($mqtt->proc()) {

        }

        $mqtt->close();

    }

    function onMessageInfo($topic,$msg){
        //echo 'Msg Recieved: ' . date('r') . "\n";
        // echo "Topic: {$topic}\n\n";
        echo $msg."\n\n";
        if($msg)
        {
            $msgs = json_decode($msg,true);
            $msgs['DTIME'] = date('Y-m-d',$msgs['TIME']);
            $tableName = 'fa_on_off_car';
            $sql = "INSERT INTO {$tableName} (IMEI,TIME,RC_TYPE,GPS_LNG,MSGID,RC_NUM,RC_TOTAL,GPS_LAT,RC_PRES,DTIME) VALUES ('{$msgs['IMEI']}','{$msgs['TIME']}','{$msgs['RC_TYPE']}','{$msgs['GPS_LNG']}','{$msgs['MSGID']}','{$msgs['RC_NUM']}','{$msgs['RC_TOTAL']}','{$msgs['GPS_LAT']}','{$msgs['RC_PRES']}','{$msgs['DTIME']}')";
            $this->db->exec($sql);
        }
    }

}

$a = new Pub();
$a->info();



